package com.cxxx.guns.modular.backend.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cxxx.guns.core.base.controller.BaseController;
import com.cxxx.guns.core.common.exception.BizExceptionEnum;
import com.cxxx.guns.core.exception.GunsException;
import com.cxxx.guns.core.shiro.ShiroKit;
import com.cxxx.guns.core.support.BeanKit;
import com.cxxx.guns.core.util.ToolUtil;
import com.cxxx.guns.modular.backend.common.Const;
import com.cxxx.guns.modular.backend.warpper.JiaXiaoWarpper;
import com.cxxx.guns.modular.system.model.User;
import com.cxxx.guns.modular.system.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cxxx.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cxxx.guns.modular.backend.model.Announcement;
import com.cxxx.guns.modular.backend.service.IAnnouncementService;

import java.util.List;

/**
 * 通知公告控制器
 *
 * @author fengshuonan
 * @Date 2018-09-05 09:31:33
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController extends BaseController {

    private String PREFIX = "/backend/announcement/";

    @Autowired
    private IAnnouncementService announcementService;


    /**
     * 跳转到通知公告首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "announcement.html";
    }

    /**
     * 跳转到添加通知公告
     */
    @RequestMapping("/announcement_add")
    public String announcementAdd() {
        return PREFIX + "announcement_add.html";
    }

    /**
     * 跳转到修改通知公告
     */
    @RequestMapping("/announcement_update/{announcementId}")
    public String announcementUpdate(@PathVariable Integer announcementId, Model model) {
        Announcement announcement = announcementService.selectById(announcementId);
        model.addAttribute("announcement", announcement);
        LogObjectHolder.me().set(announcement);
        return PREFIX + "announcement_edit.html";
    }

    /**
     * 跳转到修改通知公告
     */
    @RequestMapping("/announcement_detail/{announcementId}")
    public String announcementDetail(@PathVariable Integer announcementId, Model model) {
        Announcement announcement = announcementService.selectById(announcementId);
        model.addAttribute("announcement", announcement);
        LogObjectHolder.me().set(announcement);
        return PREFIX + "announcement_detail.html";
    }

    /**
     * 获取驾校互动列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List list = null;
        EntityWrapper<Announcement>  entityWrapper;
        Wrapper<Announcement> wrapper;
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        if ((ToolUtil.equals(userId, com.cxxx.guns.core.common.constant.Const.ADMIN_ID))) {
            if (ToolUtil.isEmpty(condition)) {
                list = announcementService.selectList(null);
                return new JiaXiaoWarpper(BeanKit.listToMapList(list)).warp();
            }
            entityWrapper = new EntityWrapper<>();
            wrapper = entityWrapper.eq("notice_title", condition);
            list = announcementService.selectList(wrapper);
            return new JiaXiaoWarpper(BeanKit.listToMapList(list)).warp();
        }else {
            if (ToolUtil.isEmpty(condition)) {
                entityWrapper = new EntityWrapper<>();
                wrapper = entityWrapper.eq("user_id", userId);
                list = announcementService.selectList(wrapper);
                return new JiaXiaoWarpper(BeanKit.listToMapList(list)).warp();
            }
            entityWrapper = new EntityWrapper<>();
            wrapper = entityWrapper.eq("notice_title", condition).eq("user_id", userId);
            list = announcementService.selectList(wrapper);
            return new JiaXiaoWarpper(BeanKit.listToMapList(list)).warp();
        }

    }

    /**
     * 新增通知公告
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Announcement announcement) {
        return announcementService.addAnnouncement(announcement);
    }

    /**
     * 删除
     * 防止横向越权
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer announcementId) {
        Announcement announcement = announcementService.selectById(announcementId);
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        if ((ToolUtil.equals(userId, announcement.getUserId())) || ToolUtil.equals(userId, 1)) {
            announcementService.deleteById(announcementId);
        }
        return SUCCESS_TIP;
    }

    /**
     * 审核
     */
    @RequestMapping(value = "/check")
    @ResponseBody
    public Object check(@RequestParam Integer announcementId) {
        Announcement announcement = announcementService.selectById(announcementId);
        announcement.setStatus(Const.JiaXiaoStatusEnum.SHEN_HE_TONG_GUO.getCode());
        announcementService.updateAllColumnById(announcement);
        return SUCCESS_TIP;
    }


    @RequestMapping(value = "/reject")
    @ResponseBody
    public Object reject(@RequestParam Integer announcementId) {
        Announcement announcement = announcementService.selectById(announcementId);
        announcement.setStatus(Const.JiaXiaoStatusEnum.SHEN_HE_SHI_BAI.getCode());
        announcementService.updateAllColumnById(announcement);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     * 防止横向越权
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Announcement announcement) {
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        Announcement anno = announcementService.selectById(announcement.getId());
        if ((ToolUtil.equals(userId, anno.getUserId()))) {
            if (ToolUtil.equals(userId, 1)) {
                announcement.setStatus(Const.JiaXiaoStatusEnum.SHEN_HE_TONG_GUO.getCode());
                announcementService.updateById(announcement);
            } else {
                announcement.setStatus(Const.JiaXiaoStatusEnum.WEI_SHEN_HE.getCode());
                announcementService.updateById(announcement);
            }
        }
        return SUCCESS_TIP;
    }

    /**
     * 通知公告详情
     */
    @RequestMapping(value = "/detail/{announcementId}")
    @ResponseBody
    public Object detail(@PathVariable("announcementId") Integer announcementId) {
        return announcementService.selectById(announcementId);
    }
}
